# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from scrapy.loader import ItemLoader

from ScrapyProject.items import BookItem


class BookSpider(scrapy.Spider):
    name = 'book'
    base_url = 'http://shicimingju.com'
    # allowed_domains = ['shicimingju.com']
    start_urls = ['http://shicimingju.com/book/sanguoyanyi.html',
                  # 'http://shicimingju.com/book/hongloumeng.html',
                  # 'http://shicimingju.com/book/xiyouji.html',
                  # 'http://shicimingju.com/book/shuihuzhuan.html',
                  ]

    def parse(self, response):
        # name = response.url.split("/")[-1]
        # self.log('Saved file %s' % name)

        # 获取所有章节的li标签
        chapters = response.xpath('//div[@class="book-mulu"]/ul/li')
        # 遍历每一个li标签，提取章节的详细网址和章节名称
        for chapter in chapters:
            # 创建/实例化Item类对象
            # item = BookItem()
            # -). 创建ItemLoader对象， 将item对象和selector/response关联
            l = ItemLoader(item=BookItem(), selector=chapter)
            detail_url = chapter.xpath('./a/@href').extract_first()
            # name = chapter.xpath('./a/text()').extract_first()
            # bookname = response.url.split('/')[-1].strip('.html')
            # item['name'] = name
            # item['bookname'] = bookname

            # -). 根据xpath进行提取数据信息并填充到item对象的name属性中
            l.add_xpath('name', './a/text()')
            # -). 将数据信息(书籍名称)填充到item对象的bookname属性中
            l.add_value('bookname', response.url.split('/')[-1].strip('.html'))

            yield Request(url=self.base_url+detail_url,
                          callback=self.parse_chapter_detail,
                          meta={'item': l.load_item()})
            # yield {
            #     'detail_url': detail_url,
            #     'name': name,
            # }
    def parse_chapter_detail(self,response):
        content = response.xpath('//div[@class="chapter_content"]').xpath('string(.)').get()
        item = response.meta['item']
        item['content'] = content
        # print(item)
        yield item

